<template>
  <div>

    <el-card class="unit-detail-card">

      <i class="iconfont icon-danwei" />
      <div class="card-panel-description">
        <div class="card-panel-text">
          单位名称：{{ currentUnit.name }}
        </div>
        <div class="card-panel-text">
          单位地址：{{ currentUnit.address }}
        </div>
      </div>
    </el-card>

    <div class="pane">
      <el-card
        shadow="hover"
        class="unit-device-card"
        @click.native="toDevice()"
      >
        <div class="content">
          <i class="iconfont icon-shebei" />
          <div class="description">
            <div class="text">
              设备总数量
            </div>
            <div class="num">
              {{ totalDeviceCount }}
            </div>
          </div>
        </div>
      </el-card>

      <el-card
        shadow="hover"
        class="unit-device-card"
        @click.native="toDevice(2)"
      >
        <div class="content">
          <i class="iconfont icon-yichang" />
          <div class="description">
            <div class="text">
              故障设备数量
            </div>
            <div class="num">
              {{ faultDeviceCount }}
            </div>
          </div>
        </div>
      </el-card>

    </div>
  </div>

</template>

<script>

import { listDevices } from '@/api/device'

export default {
  name: 'Index',
  props: {
    unit: {
      type: Object
    },
    fromMap: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      queryParams: {
        unitId: ''
      },
      currentUnit: {},

      faultDeviceCount: 0,

      totalDeviceCount: 0
    }
  },
  mounted() {
    if (this.fromMap) {
      localStorage.removeItem('currentUnit')
    }
    const currentUnit = localStorage.getItem('currentUnit')
    if (currentUnit) {
      this.currentUnit = JSON.parse(currentUnit)
      this.queryParams.unitId = this.currentUnit.unitId
    } else {
      this.currentUnit = this.$route.params.unit
      this.queryParams.unitId = this.currentUnit.unitId
      localStorage.setItem('currentUnit', JSON.stringify(this.$route.params.unit))
    }

    listDevices(this.queryParams).then(response => {
      const deviceList = response.data
      this.totalDeviceCount = deviceList.length
      deviceList.forEach(item => {
        if (item.state === 2) {
          this.faultDeviceCount++
        }
      })
    })
  },

  methods: {
    toDevice(state) {
      if (state === 2) {
        this.$router.push({
          name: 'Device',
          params: {
            unitId: this.currentUnit.unitId,
            queryState: 2
          }
        })
      } else {
        this.$router.push({
          name: 'Device',
          params: {
            unitId: this.currentUnit.unitId
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.unit-detail-card {
  .card-panel-description {
    float: left;
    font-weight: bold;
    margin: 26px;
    margin-left: 0px;
    .card-panel-text {
      line-height: 18px;
      color: rgba(0, 0, 0, 0.45);
      font-size: 16px;
      margin-bottom: 12px;
    }

  }

}
.pane {
  display: flex;
  flex: initial;
  .content {
    display: flex;
    flex: inherit;
    .description {
      margin: 25px 10px 20px 10px;
      .text {
        font-weight: bold;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 15px;
      }
      .num {
        font-size: 20px;
        font-weight: bold;
      }
    }
  }
}
.icon-danwei {
  font-size: 90px;
  float: left;
  margin-right: 50px;
  margin-left: 50px;
  width: 100px;
  height: 100px;
}
.icon-shebei {
  font-size: 90px;
}
.icon-yichang {
  font-size: 90px;
}
</style>
